//获取url的参数，可以解决乱码问题
window.getRequest = function(name){  
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var SEARCH = window.location.href.split('?')[1] ? window.location.href.split('?')[1] : '';
    var r = SEARCH.match(reg);
    if(r!=null){
        //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
        return  decodeURI(r[2]);
    };
    return null;                                                
}

//日期时间不够两位自动补0
window.getZero = function(num){
    if(num<10){
        return '0' + num;
    }
    return num;
}

//转化时间
window.conversionTime = {
    //转化时间2019-01-28T07:39:00.000Z 转化成xxxx-xx-xx xx:xx:xx
    timeToYMdHms:function(str){
        let d = new Date(str);
        return d.getFullYear() + '-' +getZero(d.getMonth() + 1) + '-' + getZero(d.getDate()) + ' ' + getZero(d.getHours()) + ':' + getZero(d.getMinutes())+':'+getZero(d.getSeconds()); 
    },
    //转化时间2019-01-28T07:39:00.000Z 转化成xxxx-xx-xx xx:xx
    timeToYMdHm:function(str){
        let d = new Date(str);
        return d.getFullYear() + '-' +getZero(d.getMonth() + 1) + '-' + getZero(d.getDate()) + ' ' + getZero(d.getHours()) + ':' + getZero(d.getMinutes()); 
    },
    //转化时间2019-01-28T07:39:00.000Z 转化成xxxx-xx-xx
    timeToYMd:function(str){
        let d = new Date(str);
        return d.getFullYear() + '-' +getZero(d.getMonth() + 1) + '-' + getZero(d.getDate()); 
    },
    //转化时间2019-01-28T07:39:00.000Z 转化成xxxx年xx月xx日
    timeToYMdC:function(str){
        let d = new Date(str);
        return d.getFullYear() + '年' +getZero(d.getMonth() + 1) + '月' + getZero(d.getDate())+ '日'; 
    },
    //转化格式 xx月xx日 xx:xx
    timeToMdHmChinese:function(str){
        if(str){
            let d = new Date(str);
            return getZero(d.getMonth() + 1) + '月' + getZero(d.getDate())+'日 '+getZero(d.getHours()) + ':' + getZero(d.getMinutes());
        }
        return ''
    },
    //格式 xx:xx
    timeToHm:function(str){
        if(str){
            let d = new Date(str);
            return getZero(d.getHours()) + ':' + getZero(d.getMinutes());
        }
        return ''
    },
    //两个时间相减得分钟 time2-time1
    reduceTimeToMinute:function(time1,time2){
        if(time1 && time2){
            let timestamp1 = (new Date(time1)).getTime()
            let timestamp2 = (new Date(time2)).getTime()
            return Math.round((timestamp2-timestamp1)/1000/60)
        }
        return ''
    },
    //两个时间相减得天数（向上取整） time2-time1
    reduceTimeToDay:function(time1,time2){
        if(time1 && time2){
            let timestamp1 = (new Date(time1)).getTime()
            let timestamp2 = (new Date(time2)).getTime()
            return Math.ceil((timestamp2-timestamp1)/1000/60/60/24)
        }
        return ''
    }
}

//解决关闭对话框时，body的style样式还存留的问题
window.clearBodyStyle = function(){
    document.body.style = ''
}

//过滤
window.xFilter = {
    //过滤前后空格
    filterTrim:function(str){
        if(!str){
            return str;
        }
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    //过滤所有空格
    filterAllSpace(str){
        if(!str){
            return str;
        } 
        return String(str).replace(/\s/g, "");
    },
    //过滤表情
    filterExpress(str){
        if(!str){
            return str;
        }
        return String(str).replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
    }
}

//手机号码脱敏方法
window.xDataDesensitization = function(num) {
    var len = num.length
    if(len && len > 4) {
        return num.substring(0, 3) + '****' + num.substring(len - 4, len)
    }
}
//名字脱敏方法
window.xNameDesensitization = function(name) {
    let newStr;
    let length = name.length;
    if (length === 2) {
        newStr = name.substr(0, 1) + '*';
    }
    if (length > 2) {
        let char = '';
        for (let i = 0, len = length - 2; i < len; i++) {
            char += '*';
        }
        newStr = name.substr(0, 1) + char + name.substr(-1, 1);
    }
    return newStr;
}